<?php

namespace app\job;

/**
 * 修改商品分享赚价格队列
 */

use app\admin\model\Config;
use app\model\Goods;
use app\model\GoodsType;
use app\model\User;
use libs\Redis;
use think\facade\Log;
use think\queue\Job;
use util\Logger;

class UpdateGoodsSharePrice
{
    public function fire(Job $job, $data)
    {
        $ratio = $data['ratio'];
        try {
            Goods::field('id, commission')->chunk(100, function ($goods) use ($ratio) {
                $save = [];
                foreach ($goods as $item) {
                    $commission       = $item['commission'];
                    $share_commission = round($commission * ($ratio / 100));
                    $save[]           = [
                        'id'               => $item['id'],
                        'share_commission' => $share_commission,
                    ];
                }
                if ($save) {
                    (new Goods())->saveAll($save);
                }
            });
            $job->delete();

        } catch (\Exception $e) {
            Logger::error('修改商品分享赚价格失败', [
                'data' => $data,
                'msg'  => $e->getMessage(),
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
        $job->delete();
    }
}
